<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$this->session('serveid',$_RQ['id']);
	}
	$serveid=$this->session('serveid');
	$room=pdo_get('jy_qiuyuyue_serve',array('id'=>$serveid,'uniacid'=>$_SESSION['uniacid']));
	if(!$room){
		exi('请选择项目！','error');
	}
		$currentyear =$_RQ['year']?:date('Y');
		$currentmonth = $_RQ['month']?:date('m');
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $currentyear + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['currentyear']=$currentyear;
		$return['currentmonth']=$currentmonth;
		$return['months']=$months;
		$return['years']=$years;	
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		if(!$year){
			$year=$currentyear;
		}
		if(!$month){
			$month=$currentmonth;
		}
		$return['calendar'] =$return['kongday']= array();		
		$return['room'] = $room;
		$return['on_days']=array('0'=>'周日','1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六');
		exi($return);
}
if($_RQ['op']=='dates'){
	$serveid=$this->session('serveid');
	$room=pdo_get('jy_qiuyuyue_serve',array('id'=>$serveid,'uniacid'=>$_SESSION['uniacid']));
	$work_time=$room['work_time'];
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$startime=strtotime($year.'-'.$month);
		$endtime=strtotime($year.'-'.$month.'-'.$day);
		$nowtime=array($startime,$endtime);
		$calendar=pdo_getall('jy_qiuyuyue_serve_times',array('serveid'=>$serveid,'ontime between'=>$nowtime,'uniacid'=>$_SESSION['uniacid']),array('serveid','is_close','onday'),'onday');
		if (empty($calendar)) {
			$calendar = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date=$year . '-' . $month . '-' . $i;
				$weekid=date("w",strtotime($date));
				$weekid=$weekid?:7;
				$iswork=strpos($work_time,';'.$weekid.';');
				$is_close=$iswork===false?1:0;
				$calendar[date($date)] = array('is_close' =>$is_close);
				++$i;
			}
		}else{
			$result = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date = $year . '-' . $month . '-' . $i;
				$result[$date] = false;
				if (isset($calendar[$date])){
					$value = $calendar[$date];
					$is_close=$value['is_close'];
					
				}else{
					$weekid=date("w",strtotime($date));
					$weekid=$weekid?:7;
					$iswork=strpos($work_time,';'.$weekid.';');
					$is_close=$iswork===false?1:0;
				}
				$result[$date] = array(
					'onday' =>$date,
					'is_close' =>$is_close, 
				);
				++$i;
			}
			$calendar = $result;
		}
		$d=array();
		foreach($calendar as $k=>$v){
			$d[$k]=date('d', strtotime($k));
		}
		$return['calendar'] = $calendar;
		$return['d'] = $d;
		$weekday=$year . '-' . $month . '-01';
		$return['week'] = date('w', strtotime($weekday));
		$return['year']=$year;
		$return['month']=$month;
		$return['kongday']=array();
		for($i=0;$i<$return['week'];$i++){
			$return['kongday'][]=$i;
		}
		exi($return);
}
if($_RQ['op']=='set'){
	$serveid=$this->session('serveid');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('model')->check_settime($postdata);
		$onday = $postdata['onday'];
		if (empty($serveid)){
			exi('请选择项目！','error');
		}
		if (empty($onday)){
			exi('请选择日期！','error');
		}
		$onday2 = strtotime($onday);
		$year = date('Y', $onday2);
		$month = date('m', $onday2);
		$data=array_fields(array('is_close','open_total','onday','serve_time','serveid'),$postdata);
		$id=intval($postdata['id']);
		$data['ontime']=strtotime($onday);
		$data['onday']=$onday;
		$data['serve_time']=ijson_encode($data['serve_time']);
		if($id){
			pdo_update('jy_qiuyuyue_serve_times',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['serveid']=$this->session('serveid');
			pdo_insert('jy_qiuyuyue_serve_times',$data);
		}
		exi('设置成功','','/store.time_pai//year='.$year.'&month='.$month);
	}
	$onday=$_RQ['onday'];
	if (empty($onday)){
		exi('请选择日期！'.$onday,'error');
	}
	$item=pdo_get('jy_qiuyuyue_serve_times',array('onday'=>$onday,'serveid'=>$this->session('serveid'),'uniacid'=>$_SESSION['uniacid']));
	$item['onday']=$onday;
	$item['open_total']=$item['open_total']?:0;
	$item['serve_time']=$item['serve_time'] ? json_decode($item['serve_time'],true):array();
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
								array('type'=>'radio','title'=>'状态设置','name'=>'is_close','options'=>'is_close','help'=>'关闭后，当天项目不营业！'),
								array('type'=>'switch','title'=>'启用库存模式？','name'=>'open_total','help'=>'启用后，每个时间段每个场次，可以预约多人！'),
								array(
									'type'=>'time_box',
									'name'=>'serve_time',
									'title'=>'场次时间段',
									'add_tip'=>'添加一个时间段',
									'title1'=>'时间段',			
									'field1'=>'times',
									'title2'=>'价格',
									'field2'=>'fee',
									'field2unit'=>'元',
									'title3'=>'库存',
									'field3'=>'total',
									'field3unit'=>'人',
									'show'=>array('open_total'=>1)
								),
								array(
									'type'=>'time_box',
									'name'=>'serve_time',
									'title'=>'场次时间段',
									'add_tip'=>'添加一个时间段',
									'title1'=>'时间段',			
									'field1'=>'times',
									'title2'=>'价格',
									'field2'=>'fee',
									'field2unit'=>'元',
									'show'=>array('open_total'=>'0')
								),
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	$return['is_close']=array('开启','关闭');
	exi($return);
}
if($_RQ['op']=='batch_set'){
	$serveid=$this->session('serveid');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['price']){
			exi('请填写价格！','error');
		}
		$price = $postdata['price'];
		$year = $postdata['year'];
		$month = $postdata['month'];
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$startime=strtotime($year.'-'.$month);
		$endtime=strtotime($year.'-'.$month.'-'.$day);
		$nowtime=array($startime,$endtime);
		pdo_update('jy_qiuyuyue_serve_times',array('price'=>$price),array('serveid'=>$serveid,'ontime between'=>$nowtime,'uniacid'=>$_SESSION['uniacid']));
		$i = 1;
		while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date = $year . '-' . $month . '-' . $i;
				$isadd=pdo_get('jy_qiuyuyue_serve_times',array('onday' =>$date,'serveid'=>$serveid,'uniacid'=>$_SESSION['uniacid']));
				if (!$isadd) {
					$data=array(
						'price'=>$price,
						'onday'=>$date,
						'ontime'=>strtotime($date),
					);
					$data['uniacid']=$_SESSION['uniacid'];
					$data['serveid']=$this->session('serveid');
					pdo_insert('jy_qiuyuyue_serve_times',$data);
				}
				++$i;
		}
		exi('批量设置成功！','','/store.time_pai/');
	}
	$item=array();
	$item['year']=$_RQ['year'];
	$item['month']=$_RQ['month'];
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'price','title'=>'价格'),
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='delete'){
		$postdata=get_postdata();
		$date = trim($postdata['date']);
		if (empty($date)) {
			exi('参数错误，请选择日期！'.$date,'error');
		}
		pdo_delete('jy_qiuyuyue_serve_times',array('onday'=>$date,'serveid'=>$this->session('serveid'),'uniacid'=>$_SESSION['uniacid']));
		exi('删除成功') ;
}
if($_RQ['op']=='clear'){
		$postdata=get_postdata();
		$year = trim($postdata['year']);
		$month = trim($postdata['month']);
		$onday=$year.'-'.$month;
		pdo_delete('jy_qiuyuyue_serve_times',array('onday like'=>'%'.$onday.'%','serveid'=>$this->session('serveid'),'uniacid'=>$_SESSION['uniacid']));
		exi('清除成功') ;
}
?>
